package com.tanhua.dubbo.api;

import com.alibaba.fastjson.JSONObject;
import com.tanhua.model.mongo.Report;
import com.tanhua.model.mongo.SoulTable;
import com.tanhua.model.mongo.TestQuestions;
import com.tanhua.model.vo.TestQuestionsvo;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@DubboService

public class SoulTableApiimpl implements SoulTableApi{
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public List<TestQuestionsvo> fanall() {
        //将所有的灵魂表查询出来
        List<SoulTable> all = mongoTemplate.findAll(SoulTable.class);
        //创建一个题目表的集合
        List<TestQuestionsvo> testQuestionsvos = new ArrayList<>();
        //循环灵魂表
        for (SoulTable soulTable : all) {
            //创建vo对象
            TestQuestionsvo testQuestionsvo = new TestQuestionsvo();
            //根据灵魂表的id查询到关联的所有测试题
            Query query = Query.query(Criteria.where("linghunid").is(String.valueOf(soulTable.getId())));
            List<TestQuestions> testQuestions = mongoTemplate.find(query, TestQuestions.class);
            //将灵魂表的数据cop到vo中
            BeanUtils.copyProperties(soulTable, testQuestionsvo);
            //单独添加测试题的对象
            testQuestionsvo.setTestQuestions(testQuestions);
            testQuestionsvos.add(testQuestionsvo);
        }
        return testQuestionsvos;
    }

    @Override
    public List<String> getti(List<Map> list) {
        ArrayList<String> defen = new ArrayList<>();
        //循环list取出每一个值
        for (Map map : list) {
            String questionId = (String) map.get("questionId");
            String optionId = (String) map.get("optionId");
            //获取到一个测试题的数据
            Query query = new Query(Criteria.where("id").is(new ObjectId(questionId)));
            TestQuestions one = mongoTemplate.findOne(query, TestQuestions.class);
            if (optionId.equals("A")){
                defen.add(one.getAdefen());
            }else if (optionId.equals("B")){
                defen.add(one.getBdefen());
            }else if (optionId.equals("C")){
                defen.add(one.getCdefen());
            }else if (optionId.equals("D")){
                defen.add(one.getDdefen());
            }
        }
        return defen;
    }

    /*@Override
    public String getDengji(ArrayList<Map> answers) {
        SoulTable soulTable = new SoulTable();
        for (Map map : answers) {
            String questionId = (String) map.get("questionId");
            String optionId = (String) map.get("optionId");
            //获取到一个测试题的数据
            Query query = new Query(Criteria.where("id").is(new ObjectId(questionId)));
            TestQuestions one = mongoTemplate.findOne(query, TestQuestions.class);
            Query query1 = new Query(Criteria.where("id").is(new ObjectId(one.getLinghunid())));
            soulTable = mongoTemplate.findOne(query1, SoulTable.class);
        }
        return soulTable.getDengji();
    }*/

    @Override
    public void addreport(Long zongfen) {
        Report report = new Report();
        if (zongfen <= 20){
            //狐狸型
            report.setConclusion("狐狸型 ：人际关系能力极强，擅长以口语表达感受而引起共鸣，很会激励并带动气氛。他们喜欢跟别人互动，重视群体的归属感，基本上是比较「人际导向」。由于他们富同理心并乐于分享，具有很好的亲和力，在服务业、销售业、传播业及公共关系等领域中，狐狸型的领导者都有很杰出的表现。,");
            report.setKey("判断");
            report.setValue("70%");
            mongoTemplate.save(report);
        }else if (zongfen <= 40){
            //狐狸型
            report.setConclusion("猫头鹰：他们的共同特质为重计划、条理、细节精准。在行为上，表现出喜欢理性思考与分析、较重视制度、结构、规范。他们注重执行游戏规则、循规蹈矩、巨细靡遗、重视品质、敬业负责。");
            report.setKey("抽象");
            report.setValue("80%");
            mongoTemplate.save(report);
        }else if (zongfen <= 60){
            //狐狸型
            report.setConclusion("白兔型：平易近人、敦厚可靠、避免冲突与不具批判性。在行为上，表现出不慌不忙、冷静自持的态度。他们注重稳定与中长程规划，现实生活中，常会反思自省并以和谐为中心，即使面对困境，亦能泰然自若，从容应付。");
            report.setKey("理性");
            report.setValue("60%");
            mongoTemplate.save(report);
        }else{
            //狐狸型
            report.setConclusion("狮子型：性格为充满自信、竞争心强、主动且企图心强烈，是个有决断力的领导者。一般而言，狮子型的人胸怀大志，勇于冒险，看问题能够直指核心，并对目标全力以赴。他们在领导风格及决策上，强调权威与果断，擅长危机处理，此种性格最适合开创性与改革性的工作。");
            report.setKey("外向");
            report.setValue("90%");
            mongoTemplate.save(report);
         }
    }

    @Override
    public Report ChaXun(String id) {
        Query query = new Query(Criteria.where("id").is(new ObjectId(id)));
        Report one = mongoTemplate.findOne(query, Report.class);
        return one;
    }
}